package com.ym.medical.service.impl;

import com.github.pagehelper.PageInfo;
import com.ym.common.utils.page.PageDomain;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.SubjectDao;
import com.ym.medical.domain.entity.SubjectEntity;
import com.ym.medical.domain.entity.SubjectReplyEntity;
import com.ym.medical.domain.param.SubjectQueryParam;
import com.ym.medical.domain.param.SubjectUpdateParam;
import com.ym.medical.domain.vo.SubjectAnswerVo;
import com.ym.medical.domain.vo.SubjectListMobileVO;
import com.ym.medical.domain.vo.SubjectListPcVO;
import com.ym.medical.domain.vo.SubjectReplyListMobileVO;
import com.ym.medical.service.ICommonSV;
import com.ym.medical.service.ISubjectReplySV;
import com.ym.medical.service.ISubjectSV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 主题Service业务层处理
*
* @author ym
* @date 2020-08-24
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class SubjectSVImpl extends BaseSVImpl<SubjectEntity,SubjectDao> implements ISubjectSV {

	/**
	 * 查询主题列表
	 *
	 * @param queryParam 主题 查询参数
	 * @return 主题
	 */
	@Override
	public List<SubjectListPcVO> selectListPC(SubjectQueryParam queryParam) {
		List<SubjectListPcVO> subjects = mapper.selectListPC(queryParam);
		return subjects;
	}

	/**
	 * 新增主题
	 *
	 * @param updateParam 主题 新增参数
	 * @return 结果
	 */
	@Override
	public Long insertPC(SubjectUpdateParam updateParam) {
            SubjectEntity entity = updateParam.covert();
		this.insert(entity);
		return entity.getId();
	}

	/**
	 * 修改主题
	 *
	 * @param updateParam 主题 修改参数
	 * @return 结果
	 */
	@Override
	public int updateByIdPC(SubjectUpdateParam updateParam) {
            SubjectEntity entity = updateParam.covert("createTime");
		int result = this.updateById(entity);
		return result;
	}

	@Autowired
	private ISubjectReplySV subjectReplySV;

	@Autowired
    private ICommonSV commonSV;

	@Override
	public PageInfo<SubjectListMobileVO> getSubjectDetail(String answerType, Long answerId, Long loginId) {
		//主题
		PageDomain.startPage();
		PageInfo<SubjectListMobileVO> subjectModelPageInfo = new PageInfo<SubjectListMobileVO>(mapper.getSubjectDetail(answerType,answerId,loginId));
		for (SubjectListMobileVO subject : subjectModelPageInfo.getList()) {
			//主题答复
			List<SubjectReplyListMobileVO> subjectReplys = subjectReplySV.getSubjectReplyDetail(subject.getId(),loginId);
		}
		return subjectModelPageInfo;
	}

	/**
	 * 主题列表
	 * @param queryParam
	 * @return
	 */
	@Override
	public List<SubjectAnswerVo> searchList(SubjectQueryParam queryParam) {
		return mapper.searchList(queryParam);
	}


    @Override
    public SubjectAnswerVo search(SubjectQueryParam queryParam) {
        SubjectAnswerVo subjectAnswerVo = new SubjectAnswerVo();
        String sql = " select id , content from subject where deleted = '00'" +
                " and answer_id = #{params.answerId} and answer_type = #{params.answerType} order by id desc limit 1";
        Map<String, Object> params = new HashMap<>();
        params.put("answerId", queryParam.getAnswerId());
        params.put("answerType", queryParam.getAnswerType());
        SubjectEntity subject = commonSV.queryEntity(sql, params, SubjectEntity.class);
        if (subject != null) {
            subjectAnswerVo.setTitle(subject.getContent());
            // 回答查询
            sql = " select content from subject_reply where deleted = deleted = '00'"+
                    " and subject_id = #{params.subjectId} order by id desc limit 1";
            params.put("subjectId",subject.getId());
            SubjectReplyEntity subjectReply = commonSV.queryEntity(sql, params, SubjectReplyEntity.class);
            if(subjectReply!=null){
                subjectAnswerVo.setAnswer(subjectReply.getContent());
            }
        }
        return  subjectAnswerVo;
    }
}
